<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2019/3/21
 * Time: 21:03
 */

namespace App\Models;


use Illuminate\Database\Eloquent\Model;

class Shipping extends Model
{
    protected $table = 'v_shipping';

    // global_status 状态
    const GLOBAL_DEFAULT = 0; //报名完成
    const GLOBAL_CANCEL_APPLAY = -2; //-2申请违约取消运单
    const GLOBAL_CANCEL_A_STATUS = -1; //-1已违约
    const GLOBAL_DRIVER_ARRIVE_SRC = 10; // 到达发货地
    const GLOBAL_DRIVER_SHIPMENT = 15; // 司机已确认装货
    const GLOBAL_DRIVER_LOADED = 20; // 司机完成装货磅单上传
    const GLOBAL_SENDER_LOAD_CONFIRM = 25; // 发货人确认装货
    const GLOBAL_DRIVER_ARRIVE_DST = 30; // 司机确认到达【即司机点击确认交货按钮】
    const GLOBAL_DRIVER_UNLOADED = 35; // 司机完成卸货磅单上传
    const GLOBAL_RECEIVER_CONFIRM_LOSS = 40; // 收货人确认损耗
    const GLOBAL_OWNER_CONFIRM_AMOUNT = 42; // 货主端确认结算金额
    const GLOBAL_DRIVER_CONFIRM_AMOUNT = 45; // 司机确认金额
    const GLOBAL_OWNER_PAY = 50; // 财务付款完成
    const GLOBAL_DRIVER_CONFIRM_PAYED = 55; // 司机确认支付结果

    // 货运单支付状态
    const PAY_WAIT = 0;
    const PAY_DONE = 10;
    const PAY_CONFIRM = 20;

    /**
     * 不可批量赋值的属性
     * @var array
     */
    protected $guarded = ['id'];

    /**
     * 信息费付款完成 【待装货】
     * @var int $statusInfoPayComplete
     */
    public static $statusInfoPayComplete = 10;

    /**
     * 装货完成 -> 过磅完成 -> 送货中【运输中】
     * @var int $statusLoadComplete
     */
    public static $statusLoadComplete = 20;

    /**
     * 卸货完成 -> 过磅完成 -> 待付款 【待支付】
     * @var int $statusUnLoadComplete
     */
    public static $statusUnLoadComplete = 30;

    /**
     * 结算确认 【已完成】
     * @var int $statusTransportPayComplete
     */
    public static $statusTransportPayComplete = 40;

    // 状态描述
    public static $statusDesc = [
        -2 => '申请违约取消运单',
        -1 => '已违约',
        10 => '待装货',
        20 => '待送货',
        30 => '待付款',
        40 => '已结算',
    ];

    // 全局状态描述
    public static $globalDesc = [
        self::GLOBAL_DEFAULT => '报名完成',
        self::GLOBAL_CANCEL_APPLAY => '申请违约取消运单',
        self::GLOBAL_CANCEL_A_STATUS => '已违约',
        self::GLOBAL_DRIVER_ARRIVE_SRC => '到达现场',
        self::GLOBAL_DRIVER_SHIPMENT => '司机确认装货',
        self::GLOBAL_DRIVER_LOADED => '司机完成装货磅单上传',
        self::GLOBAL_SENDER_LOAD_CONFIRM => '发货人确认装货',
        self::GLOBAL_DRIVER_ARRIVE_DST => '司机确认到达',
        self::GLOBAL_DRIVER_UNLOADED => '司机完成卸货磅单上传',
        self::GLOBAL_RECEIVER_CONFIRM_LOSS => '收货人确认损耗',
        self::GLOBAL_OWNER_CONFIRM_AMOUNT => '货主确认结算金额',
        self::GLOBAL_DRIVER_CONFIRM_AMOUNT => '司机确认金额',
        self::GLOBAL_OWNER_PAY => '财务付款完成',
        self::GLOBAL_DRIVER_CONFIRM_PAYED => '司机确认支付结果',
    ];

    // 司机信息
    public function driver()
    {
        return $this->belongsTo('App\Models\Member', 'driver_uuid', 'uuid');
    }

    // 司机扩展信息
    public function driver_extend()
    {
        return $this->belongsTo('App\Models\Driver', 'driver_uuid', 'uuid');
    }

    /**
     * 运货列表状态名称显示
     * @param $statusCode
     * @return string
     */
    public static function setStatusNamesForShippingList($statusCode)
    {
        switch ($statusCode) {
            case self::$statusInfoPayComplete:
                return '待装货';
            case self::$statusLoadComplete:
                return '待送货';
            case self::$statusUnLoadComplete:
                return '待付款';
            case self::$statusTransportPayComplete:
                return '已结算';
        }

        return '未知运货状态';
    }

    /**
     * 运货列表时间类型名称显示
     * @param $statusCode
     * @return string
     */
    public static function setTimeNamesForShippingList($statusCode)
    {
        switch ($statusCode) {
            case self::$statusInfoPayComplete:
                return '接单时间';
            case self::$statusLoadComplete:
                return '装货时间';
            case self::$statusUnLoadComplete:
                return '交货时间';
            case self::$statusTransportPayComplete:
                return '结束时间';
        }

        return '未知时间';
    }

}